package com.jiawa.train.business.config;

import com.alibaba.csp.sentinel.init.InitExecutor;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.transport.HeartbeatSender;
import org.bouncycastle.asn1.its.EndEntityType;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

import java.net.InetAddress;
import java.net.Socket;
import java.security.cert.CertificateFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@SpringBootApplication
@ComponentScan("com.jiawa")
@MapperScan("com.jiawa.train.business.mapper")
@EnableCaching
public class BusinessApplication {

    public static void main(String[] args) {
        SpringApplication.run(BusinessApplication.class, args);
        // 强制覆盖端口设置

        //限流规则
        initFlowRules();
    }

    private static void initFlowRules(){
        /*ArrayList<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource("doConfirm");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);

        rule.setCount(20);
        rules.add(rule);
        FlowRuleManager.loadRules(rules);*/


        // 1. 移除冗余连接测试（Sentinel客户端会自动重连）
        // 2. 简化配置
        System.setProperty("csp.sentinel.dashboard.server", "192.168.12.1:18080");
        System.setProperty("csp.sentinel.api.port", "8719");

        // 3. 添加规则
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule("doConfirm")
                .setGrade(RuleConstant.FLOW_GRADE_QPS)
                .setCount(20);
        rules.add(rule);

        FlowRuleManager.loadRules(rules);
        System.out.println("✅ 限流规则加载完成 | 资源: doConfirm | QPS阈值: 20");


    }




}
